package com.customer.crm.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.customer.crm.system.pojo.SystemRole;
import com.customer.crm.system.pojo.SystemUser;
import com.customer.crm.system.pojo.SystemUserRole;
import com.customer.crm.system.pojo.UserAndRole;
import com.customer.crm.system.service.impl.SystemRoleServiceImpl;
import com.customer.crm.system.service.impl.SystemUserRoleServiceImpl;
import com.customer.crm.system.service.impl.SystemUserServiceImpl;
import com.customer.crm.system.service.impl.UserAndRoleServiceImpl;
import com.customer.crm.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * The type System user controller.
 */
@Controller
@RequestMapping("/system/user")
public class SystemUserController extends BaseController {
    @Autowired
    private SystemUserServiceImpl systemUserService;
    @Autowired
    private UserAndRoleServiceImpl userAndRoleService;
    @Autowired
    private SystemUserRoleServiceImpl systemUserRoleService;
    @Autowired
    private SystemRoleServiceImpl systemRoleService;
    /**
     * User string.
     * 加载展示所有的用户页面
     *
     * @return the string
     */
    @RequestMapping("/UserAction_list.action")
    public String user() {
        return "views/system/list_user";
    }

    /**
     * List users model map.
     * Ajax请求展示的用户数据
     *
     * @param page    the page
     * @param rows    the rows
     * @return the model map
     */
    @RequestMapping("/UserAction_findByPage.action")
    @ResponseBody
    public ModelMap listUsers(String page, String rows) {
        Integer p = (Integer.parseInt(page))-1;
        Integer r = Integer.parseInt(rows);
        ModelMap modelMap = new ModelMap();
        List<UserAndRole> userRolePage = userAndRoleService.findUserRolePage((p * r), r);
        List<SystemUserRole> list = systemUserRoleService.list(null);
        modelMap.put("totalRows", list.size());
        modelMap.put("result", userRolePage);
        ModelMap modelMap1 = new ModelMap();
        modelMap1.put("data", modelMap);
        return modelMap1;
    }

    /**
     * Add user string.
     * 跳转添加用户页面
     *
     * @return the string
     */
    @GetMapping("/UserAction_edit.action")
    public String addUser() {
        return "views/system/add_user";
    }

    /**
     * Save user result data.
     * 添加到数据库
     *
     * @param systemUser the system user
     * @return the result data
     */
    @RequestMapping("/saveUser")
    @ResponseBody
    public ResultData saveUser(SystemUser systemUser) {
        boolean save = systemUserService.save(systemUser);
        resultData = ResultData.buildSuccessResult("添加成功", save);
        return resultData;
    }

    @RequestMapping("/UserAction_remove.action")
    @ResponseBody
    public ResultData deleteUser(String[] ids) {
        for (String a : ids) {
            boolean b = systemUserService.removeById(a);
            resultData = ResultData.buildSuccessResult("删除成功", b);
        }
        return resultData;
    }

    /**
     * Update string.
     * 携带数据跳转修改页面
     *
     * @param id    the id
     * @param model the model
     * @return the string
     */
    @GetMapping("/UserAction_edt.action")
    public String updateUser(@RequestParam("userid") String id, Model model) {
        SystemUser systemUser = systemUserService.getOne(new QueryWrapper<SystemUser>().eq("id", id));
        model.addAttribute("userVO", systemUser);
        return "views/system/edit_user";
    }

    /**
     * Assign string.
     * 携带数据回传assign_user.html页面
     *
     * @param id    the id
     * @param model the model
     * @return the string
     */
    @GetMapping("/UserAction_assign.action")
    public String assign(@RequestParam("userid") String id, Model model) {
        SystemUser systemUser = systemUserService.getOne(new QueryWrapper<SystemUser>().eq("id", id));
        model.addAttribute("userVO", systemUser);
        return "views/system/assign_user";
    }

    /**
     * Role list model map.
     * 查询所有的角色放到data
     *
     * @return the model map
     */
    @RequestMapping("/RoleAction_findAll.action")
    @ResponseBody
    public ModelMap roleList() {
        ModelMap modelMap = new ModelMap();
        List<SystemRole> list = systemRoleService.list(null);
        modelMap.put("data", list);
        return modelMap;
    }

    /**
     * Find by user id result data.
     * 通过用户id获取这个用户的角色
     *
     * @param id the id
     * @return the result data
     */
    @GetMapping("/RoleAction_findByUserId.action")
    @ResponseBody
    public ResultData findByUserId(@RequestParam("userId") String id) {
        List<SystemRole> byUserId = systemRoleService.findByUserId(id);
        ResultData resultData = ResultData.buildSuccessResult("data", byUserId);
        return resultData;
    }

    /**
     * Update roles result data.
     * 为用户分配角色
     *
     * @param userid the userid
     * @param ids    the ids
     * @return the result data
     */
    @PostMapping("/UserAction_updateRoles.action")
    @ResponseBody
    public ResultData updateRoles(@RequestParam("userid") String userid, @RequestParam("ids") String[] ids) {
        boolean b = systemUserRoleService.deleteUserRole(userid);
        ResultData resultData = null;
        if (b == true) {
            if (ids != null && ids.length > 0) {
                for (String id : ids) {
                    Integer integer = systemUserRoleService.insertUserRole(userid, id);
                    if (integer == 1) {
                        resultData = ResultData.buildSuccessResult("message", "分配成功");
                    } else {
                        resultData = ResultData.buildSuccessResult("message", "分配失败");
                    }
                }
            }
        }
        return resultData;
    }

}
